<?php
/**
 * @Notes:【】
 * @Date: 2022-06-03 23:09
 */

namespace Yj\Generator\Core\Dao\Processor;

use Hyperf\Database\Model\Builder;
use Hyperf\Utils\Collection;
use Hyperf\Utils\Str;
use Yj\Generator\Core\Common\Template\ArgumentTemplate;
use Yj\Generator\Core\Common\Template\MethodTemplate;
use Yj\Generator\Core\Common\Template\UsesTemplate;
use Yj\Generator\Core\Dao\Config;
use Yj\Generator\Core\Dao\Template\BaseTemplate;

class MethodProcessor
{
    /**
     * @inheritdoc
     */
    public function process(BaseTemplate $baseTemplate, Config $config)
    {
        $baseTemplate->addUses(new UsesTemplate(\Hyperf\Database\Model\Builder::class));
        $baseTemplate->addUses(new UsesTemplate(\Yj\Daos\Verify::class));
        $baseTemplate->addUses(new UsesTemplate(Collection::class));

        $MethodTemplate = new MethodTemplate('searchParams');
        $MethodTemplate->addArgument(new ArgumentTemplate('params', 'Collection'));

        $searchBody= <<<EOF
        \$search = \$this->getSearch();
        \$params->check('id',function(\$id) use (\$search) {
            \$search->where('id',\$id);
        });
        return \$search;
EOF;

        $MethodTemplate->setBody($searchBody);
        $baseTemplate->setMethods($MethodTemplate);

        //lists
        $MethodTemplate = new MethodTemplate('lists');
        $MethodTemplate->addArgument(new ArgumentTemplate('params', 'Collection'));

        $listsBody= <<<EOF
        \$data = \$this->params(\$params)->orderByDesc("id")->getList();
        return \$data;
EOF;

        $MethodTemplate->setBody($listsBody);
        $baseTemplate->setMethods($MethodTemplate);


        $addBody= <<<EOF
        \$data = \$this->create(\$params->toArray());
        return \$data;
EOF;

        //add
        $MethodTemplate = new MethodTemplate('add');
        $MethodTemplate->addArgument(new ArgumentTemplate('params', 'Collection'));
        $MethodTemplate->setBody($addBody);
        $baseTemplate->setMethods($MethodTemplate);

        $editBody= <<<EOF
        \$data = \$this->params(_Collect(["id"=>\$id]))->firstOrError();
        \$data->fill(\$params->toArray());
        return \$data->save();
EOF;
        //edit
        $MethodTemplate = new MethodTemplate('edit');

        $MethodTemplate->addArgument(new ArgumentTemplate('id', 'int'));
        $MethodTemplate->addArgument(new ArgumentTemplate('params', 'Collection'));

        $MethodTemplate->setBody($editBody);
        $baseTemplate->setMethods($MethodTemplate);

        $redBody= <<<EOF
        \$data = \$this->params(_Collect(["id"=>\$id]))->firstOrError();
        return \$data;
 EOF;
        $MethodTemplate = new MethodTemplate('read');
        $MethodTemplate->addArgument(new ArgumentTemplate('id', 'int'));
        $MethodTemplate->setBody($redBody);
        $baseTemplate->setMethods($MethodTemplate);


        $delBody = <<<EOF
        \$data = \$this->params(_Collect(["id"=>\$id]))->firstOrError();
        return \$data->delete();
EOF;
        $MethodTemplate = new MethodTemplate('del');
        $MethodTemplate->addArgument(new ArgumentTemplate('id', 'int'));
        $MethodTemplate->setBody($delBody);
        $baseTemplate->setMethods($MethodTemplate);

    }


    public function getModel(Config $config)
    {
        $namespace = '\\App\\Model';
        if ($config->has('dirname') && $config->get('dirname')) {
            $namespace = $namespace . '\\' . Str::ucfirst($config->get('dirname'));
        }
        return $namespace . '\\' . Str::studly($config->get('class_name'));
    }

}